package com.example.happytime;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView jokeTV,recoTV,videoTv;
    List<Fragment>fList;
    FragmentManager fmanager;
    DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawer = findViewById(R.id.drawer);
        jokeTV = findViewById(R.id.left_tv_joke);
        recoTV = findViewById(R.id.left_tv_reco);
        videoTv = findViewById(R.id.left_tv_video);
        jokeTV.setOnClickListener(this);
        recoTV.setOnClickListener(this);
        videoTv.setOnClickListener(this);
        initFrag();
    }

    private void initFrag() {
        fList = new ArrayList<>();
        fList.add(new JokeFragment());
        fList.add(new RecoFragment());
        fList.add(new VideoFragment());
        fmanager = getSupportFragmentManager();
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0;i< fList.size();i++){
            ft.add(R.id.main_layout_center,fList.get(i));
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(0));
        ft.commit();


    }
    private void changeFrag(int pos){
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0;i< fList.size();i++){
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(pos));
        ft.commit();
        drawer.closeDrawers();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_tv_joke:
                changeFrag(0);
                break;
            case R.id.left_tv_reco:
                changeFrag(1);
                break;
            case R.id.left_tv_video:
                changeFrag(2);
                break;
        }
    }
}
